using System;
using System.Collections.Generic;

namespace Utilities.Collections
{
  public class ReverseComparer<T> : IComparer<T> where T : IComparable<T>
  {
    private static ReverseComparer<T> _defaultComparer;
    private readonly IComparer<T> _initialComparer;

    public ReverseComparer(IComparer<T> initialComparer)
    {
      _initialComparer = initialComparer;
    }

    public int Compare(T x, T y)
    {
      return -(_initialComparer.Compare(x, y));
    }

    public static ReverseComparer<T> Default
    {
      get
      {
        ReverseComparer<T> defaultComparer = _defaultComparer;
        if (defaultComparer == null)
        {
          defaultComparer = new ReverseComparer<T>(Comparer<T>.Default);
          _defaultComparer = defaultComparer;
        }
        return defaultComparer;
      }
    }
  }
}